<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Ringo Chan <334258471@qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Model;
use Think\Model;

/**
 * 项目模型
 * @author Ringo Chan <334258471@qq.com>
 */

class ProductCategoryModel extends Model {
    
    const PATH_SEPARATOR = ",";
    
    /* 自动验证规则 */
    protected $_validate = array(
        array('title', 'require', '标题不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('title', '1,80', '标题长度为1-80个字符', self::EXISTS_VALIDATE, 'length')
    );
    
    /* 自动完成规则 */
    protected $_auto = array(
        array('title', 'htmlspecialchars', self::MODEL_BOTH, 'function'),
        array('gid', 'getGroupId', self::MODEL_BOTH, 'callback'),
        array('create_time', 'getCreateTime', self::MODEL_BOTH, 'callback'),
        array('update_time', NOW_TIME, self::MODEL_BOTH),
        array('status', 'getStatus', self::MODEL_BOTH, 'callback'),
    );
    
    /**
     * 删除数据
     * @return true 删除成功， false 删除失败
     */
    public function remove(){
        $ids = I('id');
        
        if(is_array($ids)){
            $map['id'] = array('in', $ids);
        }elseif (is_numeric($ids)){
            $map['id'] = $ids;
        }
        
        return $this->where($map)->delete();
    }
    
    /**
     * 创建时间不写则取当前时间
     * @return int 时间戳
     */
    protected function getCreateTime(){
        $id = I('post.id');
        $create_time = $this->getFieldById($id, 'create_time');
        return $create_time?$create_time:NOW_TIME;
    }
    
    /**
     * 获取数据状态
     * @return integer 数据状态
     */
    protected function getStatus(){
        $id = I('post.id');
        if(empty($id)){	//新增
            $status = 1;
        }else{//更新
            $status = $this->getFieldById($id, 'status');
        }
        return $status;
    }
    
    protected function getGroupId(){
        $pid = I('post.pid');
        if (!empty($pid)) {
            $info = $this->find($pid);
            return $info['gid'];
        } else {
            return I('post.gid');
        }
    }
}
?>